package com.viewpagerindicator.sample;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;

import com.viewpagerindicator.sample.adapters.GoodsListAdapter;
import com.viewpagerindicator.sample.beans.Goods;

import org.json.JSONException;

import java.util.ArrayList;

/**
 * Created by zhao on 2015/11/8.
 */
public class GoodsListActivity extends AppCompatActivity {
    private ListView listView;
    private GoodsListAdapter adapter;
    ArrayList<Goods> goodsArrayList=new ArrayList<>();

    private View listviewBg; //列表试图
    private View progressingBg;//等待试图;
    private View errormsgBg;//错误试图;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goodslist_layout);
        byte[] responseBody=getIntent().getByteArrayExtra("goodslistjson");
        listviewBg=  findViewById(R.id.list_bg);
        progressingBg=  findViewById(R.id.progress_bg);
        errormsgBg=  findViewById(R.id.errormsg_bg);
        try {
            goodsArrayList.addAll(Goods.parseList(responseBody));
            listView= (ListView) findViewById(R.id.first_layout_listview);
            adapter=new GoodsListAdapter(this,R.layout.goods_listview_item2,goodsArrayList);
            showView(listviewBg);
            listView.setAdapter(adapter);
        } catch (JSONException e) {
            e.printStackTrace();
            showView(errormsgBg);
        }
    }
    /**
     * 显示指定的view
     * */
    public void showView(View viewToShow){
        listviewBg.setVisibility(View.GONE);
        progressingBg.setVisibility(View.GONE);
        errormsgBg.setVisibility(View.GONE);
        viewToShow.setVisibility(View.VISIBLE);
    }
    public void back(View view){
        finish();
    }
}
